package com.gexiaobao.pigeon.ui.adapter

import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.DataBindBaseViewHolder
import com.gexiaobao.pigeon.app.eventViewModel
import com.gexiaobao.pigeon.app.model.bean.FindPigeonResponse
import com.gexiaobao.pigeon.databinding.ItemFindPigeonListBinding

/**
 * @Author      : hxw
 * @Date        : 2023/8/4 14:20
 * @Describe    :
 */
class FindPigeonListAdapter(data: ArrayList<FindPigeonResponse.ListBean>) :
    BaseQuickAdapter<FindPigeonResponse.ListBean, DataBindBaseViewHolder>(R.layout.item_find_pigeon_list, data) {

    override fun convert(holder: DataBindBaseViewHolder, item: FindPigeonResponse.ListBean) {
        val binding = DataBindBaseViewHolder.getBinding(holder) as ItemFindPigeonListBinding
        binding.data = item
        binding.executePendingBindings()//防止列表抖动

        val searchWords = eventViewModel.toNoticeRingId.value.toString()
        val blueSpan = ForegroundColorSpan(ContextCompat.getColor(context, R.color.colorPrimaryDark))
        val builder = SpannableStringBuilder(item.ringId)
        val changeTextColor: Int = item.ringId.indexOf(searchWords)
        if (changeTextColor != -1) {
            builder.setSpan(blueSpan, changeTextColor, changeTextColor + searchWords.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            holder.setText(R.id.itemRingId, builder)
        } else {
            holder.setText(R.id.itemRingId, item.ringId)
        }
    }
}